/**
 * 定时器
 */
const dialogTimeout = (func, time) => {
    let timeoutId = setTimeout(() => {
        func();
        clearTimeout(timeoutId);
    }, time);
    return timeoutId;
}

/**
 * 文字消息提示框
 */
const msg = (msg, duration = 2000) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            uni.showToast({
                title: msg || "操作成功",
                icon: 'none',
                mask: true,
                duration: (duration < 100 ? 200 : duration),
                position: 'center',
                complete: function() {
                    dialogTimeout(() => {
                        close();
                        resolve();
                    }, duration);
                }
            })
        }, 100)
    })
}
/**
 * 隐藏提示框
 */
const close = () => {
    uni.hideToast();
}
/**
 * 显示加载框
 */
const loading = (msg, mask) => {
    //先清
    uni.hideLoading();
    uni.showLoading({
        mask: (!mask || mask == true),
        title: msg || '加载中'
    });
}
/**
 * 隐藏加载框
 */
const hide = () => {
    setTimeout(() => {
        uni.hideLoading();
    }, 500)
}
/**
 * 弹出对话框
 */
const alert = (title, msg, btnText = '') => {
    return new Promise((resolve, reject) => {
        uni.showModal({
            title: title || '提示',
            content: msg || '',
            showCancel: false,
            confirmText: btnText || '确定',
            success: res => {
                if (res.confirm) {
                    resolve(true);
                } else {
                    resolve(false);
                }
            }
        });
    })
}
/**
 * 弹出确认取消对话框
 */
const confirm = (title, msg) => {
    return new Promise((resolve, reject) => {
        uni.showModal({
            title: title || '提示',
            content: msg || '',
            cancelText: '取消',
            cancelColor: '#F15B2D',
            confirmText: '确定',
            confirmColor: '#22B281',
            success: res => {
                if (res.confirm) {
                    resolve(true);
                } else {
                    resolve(false);
                }
            }
        });
    })
}

export {
    msg,
    close,
    loading,
    hide,
    alert,
    confirm
};
